from util.Sql_df import SQLServer_df
from util.sql_pyodbc import SQLServer_pyodbc
from pydantic import BaseModel


# 用户登录，返回用户ID
def GetUserid(username, password):
    try:
        return \
            SQLServer_pyodbc().Query_one(
                f"SELECT users_id FROM users where username = '{username}' and pwd = '{password}'")[0]
    except:
        return None


class Userid(BaseModel):
    id: int


#  获取用户权限信息
def GetUser_acl(id: Userid):
    sql = ("select a.role_id,a.acl_id,b.parent_id,b.acl_name,b.acl_type,b.title from ("
           "select role_id,acl_id from role_acl where role_id in("
           f"select role_id from user_roles where users_id={id})) as a left join acl as b on a.acl_id=b.acl_id")
    try:
        return \
            SQLServer_df().Query(sql)
    except:
        return None


# 获取用户基础信息（比如姓名等）
def GetUser_proper(id: Userid):
    sql = f'select full_name from users where users_id={id}'
    try:
        return \
            SQLServer_df().Query(sql)
    except:
        return None

